<?php

namespace app\common\model;

use think\Model;


class Merchantgoods extends Model
{

    

    

    // 表名
    protected $name = 'merchant_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'type_data_text',
    ];


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }
    public function getTypeDataList()
    {
        return ['0' => __('Type_data 0'), '1' => __('Type_data 1')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getTypeDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type_data']) ? $data['type_data'] : '');
        $list = $this->getTypeDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }





    protected static function init()
    {
        $log=Merchantgoods::where("end_time< ".time())->where(["status"=>'1',"type_data"=>'0'])->where("card_type_id <> 0")->find();
        if(!empty($log)){
            Merchantgoods::where("end_time< ".time())->where(["status"=>'1',"type_data"=>'0'])->where("card_type_id <> 0")->update(["status"=>'0']);
        }
    }


    public function merchant()
    {
        return $this->belongsTo('Shop', 'merchant_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function card_type()
    {
        return $this->belongsTo('app\common\model\card\Type', 'card_type_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 计算用户实际付款金额
     * */
    public function actual_payment($price,$address){
        $actual_payment2=$price;
        $actual_payment=0;
        $platform=0;
        $region_divide=0;
        $list=[];
        $address=explode("-",$address);
        if($price>1){
            $list=Commissionrules::where('FIND_IN_SET("'.$address[0].'", address) or FIND_IN_SET("'.$address[0].
                $address[1].'", address) or FIND_IN_SET("'.$address[0].$address[1].$address[2].'", address)')
                ->find();
            //2023-9-4 lina 修改 start
            //if (empty($list)) return false;
            if (empty($list)){
                $list = Commissionrules::find(1);
            }
            //2023-9-4 lina 修改 end
            $platform = bcmul($price, ($list["goods_smoke"]/100),2);
            //$platform=floor(($price*$list["goods_smoke"]/100)*100)/100;//平台抽佣
            $platform=$platform<=0?0:$platform;
            $actual_payment = bcmul($platform, ($list["favourable"]/100),2);//用户优惠金额
            //$actual_payment=floor(($platform*$list["favourable"]/100)*100)/100;//用户优惠金额
            $actual_payment=$actual_payment<=0?0:$actual_payment;
            $actual_payment2=bcsub($price,$actual_payment,2);//用户实际支付金额
            $actual_payment2=$actual_payment2<=0?0:$actual_payment2;
            $region_divide= bcmul(($platform-$actual_payment), ($list["region_divide"]/100),2);
        }
        return ["price"=>$price,"actual_payment"=>$actual_payment2,"discount"=>$actual_payment,'regionservice_commission'=>$region_divide,"platform"=>$platform,"rules"=>$list,"address"=>$address[2],"address_all"=>$address[0].$address[1].$address[2]];
    }
}
